package org.shan.lf.sever.iml;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.shan.lf.common.enums.ResponseEnum;
import org.shan.lf.common.exception.LfServiceException;
import org.shan.lf.dao.UserAccountMapper;
import org.shan.lf.dao.UserDoctorMapper;
import org.shan.lf.entity.UserAccount;
import org.shan.lf.entity.UserDoctorInfo;
import org.shan.lf.enums.DoctorVerifiedState;
import org.shan.lf.pojo.form.DoctorRegisterForm;
import org.shan.lf.pojo.form.DoctorUpdateForm;
import org.shan.lf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * Created by amanda.shan on 2019-07-27.
 */
@Service
public class UserServiceIml implements UserService {

    @Autowired
    private PasswordEncoder encoder;

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserDoctorMapper userDoctorMapper;

    @Override
    public UserAccount findUserAccount(String username) {
        return userAccountMapper.selectOne(new QueryWrapper<UserAccount>().eq("username",
                username));
    }

    @Override
    public UserAccount findUserAccount(String username, Integer identity) {
        return userAccountMapper.selectOne(new QueryWrapper<UserAccount>().eq("username",
                username).eq("identity", identity));
    }

    @Override
    public void addDoctorAccount(DoctorRegisterForm registerForm) {

        UserAccount account = new UserAccount();

        Integer count = userAccountMapper.selectCount(new QueryWrapper<UserAccount>().eq(
                "username", registerForm.getUsername()));
        if (count != null && count > 0){
            throw new LfServiceException(ResponseEnum.USER_FAIL.getCode(),"用户名已经存在");
        }

        if (!checkCode(registerForm.getInvitationCode())){
            throw new LfServiceException(ResponseEnum.USER_FAIL.getCode(), "邀请码错误");
        }

        BeanUtil.copyProperties(registerForm, account);
        account.setPassword(encoder.encode(registerForm.getPassword()));
        userAccountMapper.insert(account);
    }

    @Override
    public String getCode() {
        return "123456";
    }

    @Override
    public boolean checkCode(String code) {
        return code.equals("123456");
    }

    @Override
    public void modifyDoctor(DoctorUpdateForm updateForm) {

        UserAccount userAccount = userAccountMapper.selectOne(new QueryWrapper<UserAccount>().eq(
                "username", "shan"));

        if (userAccount == null){
            throw new LfServiceException(ResponseEnum.USER_FAIL.getCode(), "用户不存在");
        }

        UserDoctorInfo userDoctorInfo =
                userDoctorMapper.selectOne(new QueryWrapper<UserDoctorInfo>().eq("account_id",
                userAccount.getId()));

        if (userDoctorInfo != null){
            BeanUtil.copyProperties(updateForm, userDoctorInfo);
            userDoctorInfo.setVerified(DoctorVerifiedState.VERIFIEDING);
            userDoctorMapper.updateById(userDoctorInfo);
        }else{
            userDoctorInfo = new UserDoctorInfo();
            userDoctorInfo.setVerified(DoctorVerifiedState.VERIFIEDING);
            userDoctorInfo.setAccount_id(userAccount.getId());
            BeanUtil.copyProperties(updateForm, userDoctorInfo);
            userDoctorMapper.insert(userDoctorInfo);
        }
    }
}
